﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class EditAccessory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AccessoryService accessoryService = new AccessoryService();
                ClassDropdownList.DataSource = accessoryService.GetAccessoryTypeList();
                ClassDropdownList.DataTextField = "Name";
                ClassDropdownList.DataValueField = "ID";
                ClassDropdownList.DataBind();

                ServiceStationService stationService = new ServiceStationService();
                StationDropdownList.DataSource = stationService.GetALLServiceStations();
                StationDropdownList.DataTextField = "Name";
                StationDropdownList.DataValueField = "ID";
                StationDropdownList.DataBind();

                //设置元器件默认值
                Accessory entity = accessoryService.GetAccessoryByID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_DB_ID]));
                if (null != entity)
                {
                    if (!entity.AccessoryClassReference.IsLoaded)
                    {
                        entity.AccessoryClassReference.Load();
                    }
                    if (!entity.ServiceStationReference.IsLoaded)
                    {
                        entity.ServiceStationReference.Load();
                    }

                    MaterialNOTxtBox.Text = entity.MaterialNO;
                    ModelNOTxtBox.Text = entity.ModelNO;
                    ClassDropdownList.SelectedValue = entity.AccessoryClass.ID.ToString();
                    DescriptionTxtBox.Text = entity.Description;
                    PriceTxtBox.Text = entity.Price.ToString();
                    StationDropdownList.SelectedValue = entity.ServiceStation.ID.ToString();
                    CountTxtBox.Text = entity.Count.ToString();
                }
            }
        }

        protected void SaveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                AccessoryService accessoryService = new AccessoryService();
                accessoryService.EditAccessory(MaterialNOTxtBox.Text, ModelNOTxtBox.Text, DescriptionTxtBox.Text,
                    Convert.ToDecimal(PriceTxtBox.Text), Convert.ToInt32(ClassDropdownList.SelectedValue),
                    Convert.ToInt32(StationDropdownList.SelectedValue), ContextService.CurrentAccount);

                Response.Write(string.Format("<script language='JavaScript'> alert( '信息修改成功！');location.href= 'ManageComponent.aspx?{0}={1}' </script> ",
                    Constants.QUERYSTR_PAGE_NO, Request.QueryString[Constants.QUERYSTR_PAGE_NO]));

                //返回
                //Response.Redirect("ManageComponent.aspx");
            }
            catch (Exception ex)
            {
                EditAccessoryMsgLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void BackBtn_Click(object sender, EventArgs e)
        {
            //返回
            Response.Redirect(string.Format("ManageComponent.aspx?{0}={1}",  Constants.QUERYSTR_PAGE_NO, Request.QueryString[Constants.QUERYSTR_PAGE_NO]));
        }
    }
}
